package com.termux.app;

//import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.termux.R;
import com.termux.shared.termux.TermuxConstants;
//import com.termux.shared.logger.Logger;

import java.io.File;


public class MediaActivity extends AppCompatActivity {
    VideoView videoView;
    PlayerView playerView;
    SimpleExoPlayer player;

    String path;
    Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media);

        path = getIntent().getStringExtra("path");
//        Logger.logDebug();
//        Log.d("cxh__", getIntent().getStringExtra("uri"));

        uri = Uri.fromFile(new File(path));
//        Log.d("cxh__", uri.toString());

//        uri = Uri.parse(getIntent().getStringExtra("uri"));

        player = new SimpleExoPlayer.Builder(MediaActivity.this).build();
        playerView = findViewById(R.id.video_view);
//        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(MediaActivity.this,
//            Util.getUserAgent(MediaActivity.this, TermuxConstants.TERMUX_PACKAGE_NAME));
//        //这是一个代表将要被播放的媒体的MediaSource
//        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
//            .createMediaSource(uri);
//          创建mediaItem
        MediaItem mediaItem = MediaItem.fromUri(uri);
        // 设置mediaItem
        player.setMediaItem(mediaItem);
        playerView.setPlayer(player);
        //  准备播放
//        player.prepare(videoSource);
        player.setPlayWhenReady(true);
        player.prepare();
        // 开始播放
//        player.play();


//        jzvdStd.setUp(path, "vvc video", Jzvd.SCREEN_NORMAL);

        //        videoView = findViewById(R.id.videoView);
//
//        Uri uri = Uri.fromFile(new File(path));
//        videoView.setVideoURI(uri);
//
//        // 设置视频播放准备完成时的监听
//        videoView.setOnPreparedListener(mediaPlayer -> videoView.start());
//
//
//        MediaController mediaController = new MediaController(this);
//
//        videoView.setMediaController(mediaController);
//        mediaController.setMediaPlayer(videoView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 准备播放器并播放视频
    }


    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.release();
        }
    }


    @Override
    protected void onPause() {
        super.onPause();

    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        new AlertDialog.Builder(this)
            .setTitle("是否要保存h265文件？")
            .setMessage(path)
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 不删除

                    finish();
                }
            })
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Log.v("cxh__exi", String.valueOf(new File(path).exists()));
                    boolean delete = new File(path).delete();
                    Log.v("cxh__", String.valueOf(delete));
                    finish();
                }
            })
            .create()
            .show();
    }
}
